﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;
using System.Globalization;
using System.Web.UI;

namespace WaiGaoQiaoWeb.Controllers
{
    [WaiGaoQiaoWeb.FilterSession.AuthorizeAttribute]
    public class OutstockGroupController : Controller
    {
        //
        // GET: /OutstockGroup/

        /// <summary>
        /// 食材出库主表
        /// </summary>

        private OutstockDetailedInfo outStockDetail = new OutstockDetailedInfo(true);

        public ActionResult OutstockGroupIndex()
        {
            ViewData["GetWarehouse"] = GetWarehouse(-1, "请选择...");
            ViewData["GetGroup"] = GetGroup(-1, "请选择...");
            return View();
        }
        /// <summary>
        /// 保存食材入库
        /// </summary>
        /// <param name="ivm"></param>
        /// <returns></returns>
        public ActionResult SaveGroupDetail(OutstockGroupDetailedVM ivm)
        {
            if (ModelState.IsValid)
            {
                OutstockDetailedInfo outDetail = new OutstockDetailedInfo();
                outDetail.Count = ivm.Count;
                outDetail.Goods_ID = ivm.Goods_ID;
                outDetail.Group_ID = ivm.Group_ID;
                outDetail.ID = ivm.ID;
                outDetail.LeadMan = ivm.LeadMan;
                outDetail.OperatorID = ivm.OperatorID;
                outDetail.OrderNo = ivm.OrderNo;
                outDetail.Price = ivm.Price;
                outDetail.Warehouse_ID = ivm.Warehouse_ID;
                outDetail.Amount = ivm.Amount;
                outDetail.OutstockDt = Convert.ToDateTime(ivm.OutstockDt1);
                outDetail.RecordDateTime = Convert.ToDateTime(ivm.RecordDateTime1);
                outStockDetail.SaveOutstockDetailed(outDetail);
            }
            return Json(true);
        }
        /// <summary>
        /// 读取食材入库明细
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public JsonResult GetDetailList(PageVM pvm)
        {
            #region 注释代码
            //string temp = string.Empty;
            //if (!string.IsNullOrEmpty(pvm.query))
            //{
            //    string[] condition = pvm.query.Split(',');
            //    if (!string.IsNullOrWhiteSpace(condition[0]))
            //    {
            //        temp += "AND OutstockDt='" + condition[0] + "'";
            //    }
            //    if (!string.IsNullOrWhiteSpace(condition[1]))
            //    {
            //        temp += " AND Goods_ID='" + condition[1] + "'";
            //    }
            //}
            //else
            //{
            //    temp += "  AND OrderNo = ''";
            //}
            #endregion
            if (string.IsNullOrEmpty(pvm.query))
            {
                pvm.query = "and OrderNo = ''";
            }
            string condition = pvm.query;//"and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = outStockDetail.GetOutstockDetailedCount(condition);

            IList<OutstockDetailedInfo> list = outStockDetail.GetAllOutstockDetailed(condition, pvm.rp, pvm.page);

            List<string> v;
            foreach (OutstockDetailedInfo ida in list)
            {
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.OutstockDt.ToString("yyyy-MM-dd"));
                //v.Add(ida.Warehouse_ID.ToString());
                //v.Add(ida.Group_ID.ToString());
                //v.Add(ida.Goods_ID.ToString());
                v.Add(WarehouseInfo.GetWarehouseName(ida.Warehouse_ID));
                v.Add(GroupInfo.GetGroupNameById(ida.Group_ID));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.Count.ToString());
                v.Add(ida.Price.ToString());
                v.Add(ida.Amount.ToString());
                v.Add(ida.LeadMan.ToString());
                v.Add(ida.RecordDateTime.ToString());
                v.Add(ida.OperatorID.ToString());
                v.Add(ida.OrderNo.ToString());
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }


        /// 读取食材入库明细
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetDetailListByOrderNo(PageVM pvm)
        {
            string condition = "and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = outStockDetail.GetOutstockDetailedCount(condition);

            IList<OutstockDetailedInfo> list = outStockDetail.GetAllOutstockDetailed(condition, pvm.rp, pvm.page);

            List<string> v;
            foreach (OutstockDetailedInfo ida in list)
            {
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.OutstockDt.ToString("yyyy-MM-dd"));
                //v.Add(ida.Warehouse_ID.ToString());
                //v.Add(ida.Group_ID.ToString());
                //v.Add(ida.Goods_ID.ToString());
                v.Add(WarehouseInfo.GetWarehouseName(ida.Warehouse_ID));
                v.Add(GroupInfo.GetGroupNameById(ida.Group_ID));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.Count.ToString());
                v.Add(ida.Price.ToString());
                v.Add(ida.Amount.ToString());
                v.Add(ida.LeadMan.ToString());
                v.Add(ida.RecordDateTime.ToString());
                v.Add(ida.OperatorID.ToString());
                v.Add(ida.OrderNo.ToString());
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);

        }
        /// <summary>
        /// 删除食材入库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int Id)
        {
            int rowcount = outStockDetail.DeleteOutstockDetailed(Id);
            return Json(rowcount);
        }
        public ActionResult OutstockGroupEdit(int? id)
        {
            OutstockGroupDetailedVM detailVM = new OutstockGroupDetailedVM();

            ViewData["GetWarehouse"] = GetWarehouse(0, "");
            ViewData["GetGroup"] = GetGroup(0, "");
            //SelectListItem sli = new SelectListItem() { Text = "请选择...", Value = "0" };
            //IList<SelectListItem> se = new List<SelectListItem>();
            //se.Add(sli);
            //ViewData["Price"] = se;
            if (id == null)
            {
                detailVM.ID = 0;
                detailVM.OutstockDt1 = DateTime.Now.ToString("yyyy-MM-dd");
                detailVM.OrderNo = outStockDetail.BuliderOrderNo();// .BuliderOrderNo();
                detailVM.RecordDateTime1 = DateTime.Now.ToString("yyyy-MM-dd");
                detailVM.Amount = 0.00M;
                detailVM.Price = 0.00M;
                detailVM.OperatorID = ((LogOnModel)System.Web.HttpContext.Current.Session["user"]).LogName;
                detailVM.AddEdit = "Add";
                SelectListItem sli = new SelectListItem() { Text = "请选择...", Value = "0" };
                IList<SelectListItem> se = new List<SelectListItem>();
                se.Add(sli);
                ViewData["Price"] = se;
            }
            else
            {
                OutstockDetailedInfo detailInfo = outStockDetail.GetAllOutstockDetailed(Convert.ToInt32(id));
                if (detailInfo != null)
                {
                    Mapper.CreateMap<OutstockDetailedInfo, OutstockGroupDetailedVM>();
                    detailVM = Mapper.Map<OutstockDetailedInfo, OutstockGroupDetailedVM>(detailInfo);
                    detailVM.OutstockDt1 = detailInfo.OutstockDt.ToString("yyyy-MM-dd");
                    detailVM.RecordDateTime1 = detailInfo.RecordDateTime.ToString("yyyy-MM-dd");
                    detailVM.OldCount = detailInfo.Count;
                    detailVM.AddEdit = "Edit";
                    SelectListItem sli = new SelectListItem() { Text = detailVM.Price.ToString(), Value = detailVM.Price.ToString() };
                    IList<SelectListItem> se = new List<SelectListItem>();
                    se.Add(sli);
                    ViewData["Price"] = se;
                }

            }
            return View(detailVM);
        }


        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetWarehouse(int value, string text)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            WarehouseInfo wareinfo = new WarehouseInfo(true);
            IList<WarehouseInfo> warelist = wareinfo.SelWarehouseInfo(100, 1, "");
            if (text != "")
            {
                dic.Add(value, text);
            }
            foreach (WarehouseInfo wi in warelist)
            {
                dic.Add(wi.ID, wi.Warehouse_Name);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }
        /// <summary>
        /// 获取班次
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroup(int value, string text)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            GroupInfo ginfo = new GroupInfo(true);
            IList<GroupInfo> grouplist = ginfo.SelGroupInfo(100, 1, "");
            if (text != "")
            {
                dic.Add(value, text);
            }
            foreach (GroupInfo gi in grouplist)
            {
                dic.Add(gi.ID, gi.Group_Name);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }
        /// <summary>
        /// 出库验证
        /// </summary>
        /// <returns></returns>
        public ActionResult OutStockYzh(int Con_GoodsInfoId, decimal Count, decimal Price, string AddEdit, decimal OldCount)
        {
            bool istongguo = outStockDetail.OutStockYzh(Con_GoodsInfoId, Count, Price, AddEdit, OldCount);
            return Json(istongguo);
        }

        /// <summary>
        /// 出库验证
        /// </summary>
        /// <returns></returns>
        public ActionResult OutStockYzh1(string sel)
        {
            string[] strSel = sel.Split(',');
            bool istongguo = outStockDetail.OutStockYzh(Convert.ToInt32(strSel[0]), Convert.ToDecimal(strSel[1]), Convert.ToDecimal(strSel[2]), strSel[3].ToString(), Convert.ToDecimal(strSel[4]));
            return Json(istongguo);
        }
    }
}
